package com.etc.aop;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  1-> 自定义类实现HandlerInterceptor接口
 */
public class MyInterceptor implements HandlerInterceptor {

    /**
     * preHandle方法，请求到达目标之前的拦截方法
     * @param request　HttpServletRequest
     * @param response　HttpServletResponse
     * @param handler　handler
     * @return  true 放行
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("pre: "+System.currentTimeMillis());
        //handler =->com.etc.controller.AopController#test1() 后端自定义控制器
        System.out.println("preHandle ... "+handler);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle: "+System.currentTimeMillis());
        System.out.println("postHandle ... "+handler+", : modelAndView: "+modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion ... "+handler);
    }
}
